package com.kinsin.kinsinlogsystem;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LogFileCollector {
    
    public List<File> collectSortedLogFiles(String baseLogPath) {
        File baseDir = new File(baseLogPath);
        List<File> result = new ArrayList<>();
        List<File> toSort = new ArrayList<>();

        if (baseDir.exists() && baseDir.isDirectory()) {
            File[] files = baseDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile()) {
                        if ("Log.log".equals(file.getName())) {
                            // Log.log 作为第一个元素
                            result.add(file);
                        } else if (file.getName().startsWith("tmpFile_")) {
                            // 收集临时文件
                            toSort.add(file);
                        }
                    } else if (file.isDirectory() && file.getName().startsWith("backUp")) {
                        // 处理备份目录
                        collectBackupLogs(file, toSort);
                    }
                }
            }
        }

        // 按文件名中的时间戳排序
        Collections.sort(toSort, new LogFileComparator());

        // 合并结果
        result.addAll(toSort);
        return result;
    }

    private void collectBackupLogs(File backupDir, List<File> collector) {
        File[] subFiles = backupDir.listFiles();
        if (subFiles != null) {
            for (File subFile : subFiles) {
                if (subFile.isFile() 
                        && subFile.getName().startsWith("log") 
                        && subFile.getName().endsWith(".log")) {
                    collector.add(subFile);
                }
            }
        }
    }

    private static class LogFileComparator implements Comparator<File> {
        private final Pattern tmpFilePattern = Pattern.compile("tmpFile_(\\d{8})_(\\d{6})\\.log");
        private final Pattern logFilePattern = Pattern.compile("log(\\d{8})_(\\d{6})\\.log");

        @Override
        public int compare(File f1, File f2) {
            return Long.compare(extractTimestamp(f1), extractTimestamp(f2));
        }

        private long extractTimestamp(File file) {
            String fileName = file.getName();
            try {
                Matcher matcher = tmpFilePattern.matcher(fileName);
                if (matcher.matches()) {
                    return parse(matcher.group(1), matcher.group(2));
                }

                matcher = logFilePattern.matcher(fileName);
                if (matcher.matches()) {
                    return parse(matcher.group(1), matcher.group(2));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return 0;
        }

        private long parse(String date, String time) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            return sdf.parse(date + time).getTime();
        }
    }
}